<?php 
class CashcouponAction extends BaseAction {
	
	
	/**
	 *查询代金券
	 */
	public function index() {
		$partner_id = $_REQUEST['partner_id'];
		$goods_id = $_REQUEST['goods_id'];
		$enable = $_REQUEST['enable'];
		$is_canceled = $_REQUEST['is_canceled'];
		
		if(!empty($goods_id)){
			$map['rule_data'] = array( 'like' , "%G_".$goods_id."%" );
		}else{
			if(!empty($partner_id)){
				$map['rule_data'] = array( 'like' , "%P_".$partner_id."%" );
			}
		}
		
		if(empty($enable)) $enable='all';
		if(empty($is_canceled)) $is_canceled='all';

		if($enable!='all'){
			$map['enable']= $enable;
		}
		if($is_canceled!='all'){
			$map['is_canceled']= $is_canceled;
		}
		
		
		$datapage = M('Cashcoupon')->kPageQuery($map,$order,20);
		
		$this->assignRequestParams();
		$this->assign( 'datapage', $datapage );
		$this->display();

	}
	
	
	/**
	 *查询代金券号码
	 */
	public function code() {
		$passcode = $_REQUEST['passcode'];
		$cashcoupon_id = $_REQUEST['cashcoupon_id'];
		
		if(!empty($cashcoupon_id)){
			$map['cashcoupon_id'] = $cashcoupon_id;
		}
		
		if(!empty($passcode)) $map['passcode'] = $passcode;
		//dump($map);
		$datapage = M('CashcouponCode')->kPageQuery($map,$order,20);
		//dump($datapage);
		$this->assignRequestParams();
		$this->assign( 'datapage', $datapage );
		$this->display();

	}
	
	/**
	 *查询代金券种类（规则分类）
	 */
	public function rule() {
		
		$datapage = M('CashcouponRule')->kPageQuery($map,$order,20);
		
		$this->assignRequestParams();
		$this->assign( 'datapage', $datapage );
		$this->display();

	}
	
	public function addRule() {
			$this->display('editrule');
	}
	
	public function doAddRule() {
		$map['sort_code'] = $_POST['sort_code'];
		$map['rule'] = $_POST['checkrule'];
		$map['desc'] = $_POST['desc'];
		if(empty($map['sort_code']))exit(json_encode(array('status'=>'0','title'=>'','msg'=>'未填写标示！'))); 
		if(empty($map['rule']))exit(json_encode(array('status'=>'0','title'=>'','msg'=>'未选择验证规则！'))); 
		
		$map['ctime'] = time();// int(10) unsigned NOT NULL COMMENT '创建时间',

		$id=M('CashcouponRule')->add($map);
		//检查用户输入的code是否已经存在
		if (empty($id)) {
				exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'添加失败！'))); 
		}
		exit(json_encode(array('status'=>'1','title'=>'','msg'=>'添加成功！'))); 
	}
	/**
	 *生成代金券
	 */
	public function createCC() {
		$this->display();
	}
	
	public function editCC(){
		$id = $_GET['id'];
		if(empty($id))$this->error('参数错误！');
		$cashcoupon = M('Cashcoupon')->where('id='.$_GET['id'])->find();
		if(empty($partner))$this->error('代金券不存在！');
		$this->assign('type', 'edit');
		$this->assign('cashcoupon', $cashcoupon);
		$this->display('createCC');
	}
	
	public function doCreateCC() {
		$map['title'] = $_POST['title'];
		$map['sort_code'] = $_POST['sort_code'];
		$map['amount'] = intval( $_POST['amount'] );
		$map['total_no'] = intval( $_POST['total_no'] );
		$total_no = 	intval( $_POST['total_no'] );
		$map['etime'] 	= paramDate( $_POST['etime'] );
		$map['stime'] 	= paramDate( $_POST['stime']  );
				
		$map['enable'] 	=	$_POST['enable']=="Y"?'Y':'N';
		$map['min_amount'] = intval( $_POST['min_amount'] );	
		
		$checkdata = array(	'enable'=>'是否可用','title'=>'名称','amount'=>'面值','stime'=>'开始时间','etime'=>'结束时间','total_no'=>'总数'
							,'sort_code'=>'类型','min_amount'=>'最低消费金额');
		foreach($checkdata as $key=>$vo){
			if(empty($map[$key]))$this->error("“".$vo.'”不能为空');
		}
		//echo $map['etime'].' -- '.$map['stime'];
		//echo ($map['etime'] > $map['stime']);
		if( $map['stime'].'' >= $map['etime'].'' )$this->error('结束时间需大于开始时间');
		//echo $map['sort_code'];
		if($map['sort_code']=='cashcoupon_public'){
			
		}else if($map['sort_code']=='cashcoupon_partner'){
			if(empty($_POST['partner_id'])){
				$this->error('请选择商户');
			}
		}else if($map['sort_code']=='cashcoupon_goods'){
			if(empty($_POST['partner_id'])){
				$this->error('请选择商户');
			}
			if(empty($_POST['goods_id'])){
				$this->error('请选择商户');
			}
		}else{
			$this->error('代金券类型错误');
		}
		
		$rule_data['min_amount'] 	= "A_".$map['min_amount'];
		$rule_data['partner_id'] 	= "P_".$_POST['partner_id'];
		$rule_data['goods_id'] 		= "G_".$_POST['goods_id'];
		$map['rule_data'] 	= serialize($rule_data);
		unset($map['min_amount']);
		//dump($map);
		//exit;
		$map['ctime'] 	=	time();
		$Cashcoupon = M('Cashcoupon');
		$id = $Cashcoupon->add($map);
		unset($map);
		if(!empty($id)){
			$count=0;
			//生成代金券券码
			do{
				unset($map);
				$map['cashcoupon_id'] 	= $id;// bigint(20) unsigned NOT NULL COMMENT '主键，自增',
			  	$map['passcode'] 	= time().rand(100000, 999999);;//  varchar(20) NOT NULL COMMENT '代金券名称',
			  	$map['is_used'] 	= 'N';// enum('Y','N','P') NOT NULL DEFAULT 'N' COMMENT '是否已使用 Y-已使用 N-未使用 p-处理中，待使用',
			  	$map['order_id'] 	= '';// bigint(20) unsigned COMMENT '订单ID，对应将要使用的订单ID',
			  	$map['enable'] 		= 'Y';// enum('Y','N') NOT NULL default 'Y' COMMENT '是否可用（暂时关闭）',
			  	$map['is_delete'] 	= 'N';// enum('Y','N') NOT NULL default 'N' COMMENT '是否删除',
			  	$map['ctime'] 		= time();// int(10) unsigned NOT NULL COMMENT '创建时间',
				$id1=M('CashcouponCode')->add($map);
				if(!empty($id1)){
					$count++;
				}
			}while($total_no>$count);
			
			$this->assign('jumpUrl',U('/Cashcoupon/index'));
			$this->success('新增成功！');
		}else{
				
		}
		$this->error('新增失败！');
	}
	
	public function del() {
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$m_cashcoupon = M('Cashcoupon');
		$map['id']	= array('in', $_POST['ids']);
		$data['is_canceled']='Y';
		$no = $m_cashcoupon->where($map)->save($data);
		$map1['cashcoupon_id']=array('in', $_POST['ids']);
		$data1['is_delete']='Y';
		M('CashcouponCode')->where($map1)->save($data1);
		echo $no ? '1' : '0';
	}
}